﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Settings;

namespace djs_mp3_manager.Nastroje.MoznostiTabs
{
    public partial class Genres : UserControl,ISettingable<Genres.TypesForChange>
    {
        public Genres()
        {
            InitializeComponent();
        }

        public enum TypesForChange
        {
            GenreTree
        }

        private static List<TypesForChange> changed = new List<TypesForChange>();

        public event OnChangeDelegate OnChangeEvent;
        private ComboBox genreBox = null;
        private List<string> usedGenres = null;

        public TypesForChange AddNameOfChanged
        {
            set
            {
                if (!changed.Contains(value))
                {
                    changed.Add(value);
                }
                if (OnChangeEvent != null)
                {
                    OnChangeEvent();
                }
            }
        }

        public List<TypesForChange> ListOfChanged
        {
            get
            {
                return changed;
            }
        }

        public TreeNodeCollection GenreTree
        {
            get
            {
                return genreTree.Nodes;
            }
        }

        public void RemoveChanged()
        {
            changed.Clear();
        }

        private void Genres_Load(object sender, EventArgs e)
        {
            changed = new List<TypesForChange>();
            usedGenres = new List<string>();

            #region GenreBox init
            genreBox = new ComboBox();
            genreBox.Visible = false;
            genreBox.DropDownStyle = ComboBoxStyle.DropDown;
            genreBox.AutoCompleteMode = AutoCompleteMode.Suggest;
            genreBox.AutoCompleteSource = AutoCompleteSource.ListItems;
            genreBox.KeyDown += new KeyEventHandler(genreBox_KeyDown);
            genreTree.Controls.Add(genreBox);
            #endregion
            
            genresTab.ItemSize = new Size((genresTab.Size.Width / genresTab.TabPages.Count) - 2, genresTab.ItemSize.Height);

            #region LoadGenreTreeCollection
            genreTree.Nodes[0].Nodes.AddRange(Setting.Genres.GetTreeNodeArrayFromTreeNodeCollection(Setting.Genres.GenreList));
            genreTree.Nodes[0].Expand();
            #endregion
        }

        private void genreTree_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                TreeNode node = genreTree.GetNodeAt(e.X, e.Y);
                if (node != null)
                {
                    genreTree.SelectedNode = node;
                    if (node.Name == "libraryRootNode")
                    {
                        deleteGenreFolderToolStripMenuItem.Enabled = false;
                        editGenreFolderToolStripMenuItem.Enabled = false;
                    } else {
                        deleteGenreFolderToolStripMenuItem.Enabled = true;
                        editGenreFolderToolStripMenuItem.Enabled = true;
                    }
                    contextMenuStrip1.Show(Form.MousePosition.X, Form.MousePosition.Y);

                }
            }

            if (e.Button == MouseButtons.Left)
            {
                AddGenreToTree();
            } 
            
        }
        
        private void addNewGenreFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddNameOfChanged = TypesForChange.GenreTree;
            TreeNode addedNode = genreTree.SelectedNode.Nodes.Add("");
            addedNode.StateImageIndex = 1;
            genreTree.SelectedNode.Expand();
            genreTree.SelectedNode = addedNode;
            ShowGenreBox();
        }

        private void addNewNormalFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddNameOfChanged = TypesForChange.GenreTree;
            TreeNode addedNode = genreTree.SelectedNode.Nodes.Add("");
            addedNode.Text = "folder";
            addedNode.StateImageIndex = 0;
            genreTree.SelectedNode.Expand();
            genreTree.SelectedNode = addedNode;
        }

        private void editGenreFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowGenreBox();
        }

        private void deleteGenreFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DeleteGenreInTree();
        }
        
        private void genreTree_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                DeleteGenreInTree();
            }
        }

        private void genreBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (genreBox.Visible == true)
                {
                    genreBox.Visible = false;
                }
            }
            else if (e.KeyCode == Keys.Enter)
            {
                AddGenreToTree();
            } 
        }

        private void genreTree_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (genreTree.SelectedNode != null && genreTree.SelectedNode.Name != "libraryRootNode" && genreTree.SelectedNode.Tag != null)
            {
                e.CancelEdit = true;
                ShowGenreBox();
            }
        }

        private void AddGenreToTree()
        {
            AddNameOfChanged = TypesForChange.GenreTree;
            if (genreBox.Visible == true)
            {
                if (genreTree.SelectedNode.Tag != null)
                {
                    if (genreTree.SelectedNode.Tag.ToString() != genreBox.Text.Trim().ToLower())
                    {
                        if (usedGenres.Contains(genreBox.Text.ToLower()) == false) 
                        {
                            #region Add Genre To Tree
                            if (genreBox.Text.Trim() != "")
                            {
                                usedGenres.Add(genreBox.Text.Trim().ToLower());
                                genreTree.SelectedNode.Tag = genreBox.Text.Trim().ToLower();
                            }
                            genreBox.Visible = false;
                            genreTree.SelectedNode.Text = genreBox.Text.Trim();
                            #endregion
                        } else {
                            MessageBox.Show("This genre is alrery in use.In tree cannot be 2 same genres.");
                        }
                    } else {
                        genreBox.Visible = false;
                    }
                } else {
                    if (usedGenres.Contains(genreBox.Text.Trim().ToLower()) == false)
                    {
                        #region Add Genre To Tree
                        if (genreBox.Text.Trim() != "")
                        {
                            usedGenres.Add(genreBox.Text.Trim().ToLower());
                            genreTree.SelectedNode.Tag = genreBox.Text.Trim().ToLower();
                        }
                        genreBox.Visible = false;
                        genreTree.SelectedNode.Text = genreBox.Text.Trim();
                        #endregion
                    }
                    else
                    {
                        MessageBox.Show("This genre is alrery in use.In tree cannot be 2 same genres.");
                    }
                }
            }

        }

        private void DeleteGenreInTree()
        {
            if (genreTree.SelectedNode != null && genreTree.SelectedNode.Name != "libraryRootNode")
            {
                if (genreTree.SelectedNode.Nodes.Count > 0)
                {
                    if (MessageBox.Show("This genre have some subgenres.This operation delete also this subgenres.", "Question", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        genreTree.SelectedNode.Remove();
                    }
                } else {
                    genreTree.SelectedNode.Remove();
                }
                
            }
            AddNameOfChanged = TypesForChange.GenreTree;
        }

        private void ShowGenreBox()
        {
            
            genreBox.Location = genreTree.SelectedNode.Bounds.Location;

            genreBox.Visible = true;
            genreBox.Focus();

        }

    }
}
